#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

/* @author Joseph Conrad Stack
*          jcstack on 9-20-03
*          calculator.c
*
*  this program reads in input and, depending on the
*  specified operation, computes the desired operation.
*  input:
*  >(add | sub | mult | div) int1 int2
*/


/* main method handles all calculations
*  @param line  the line of input
*  @param opptr the string equivalent of the operator used for calculation
*  @param var1  the first operand(string)
*  @param var2  the second operand(string)
*  @param i     used as a condition for the while loop
*  @param num1  the first operand(int)
*  @param num2  the second operand(int)
*  @param answer the answer to the inputed statement assuming syntax is correct
*  
*  @return returns EXIT_SUCCESS when the user's input is "quit"
*/ 
int main(){
    char line[100];
    char *opptr;
    char *var1;
    char *var2;
    int i = 0;
    int num1 = 0;
    int num2 = 0    
    int answer = 0;

    again: 
      while(i == 0){
        fgets(line, sizeof(line), stdin);
        opptr = strtok(line," ");
        
        
        if((strcmp(opptr, "quit\n"))==0){
            i=1;
            return EXIT_SUCCESS;
        }

        var1 = strtok(NULL," ");
        var2 = strtok(NULL," ");
        

        if((!var1 | !var2)){
            printf("not enough args\n");
            goto again;
        }
        else if((strcmp(opptr, "add"))==0){
            num1 = atoi(var1);
            num2 = atoi(var2);
            answer = (num1 + num2);
            printf("%d\n", answer);
        }

        else if((strcmp(opptr, "sub"))==0){
            num1 = atoi(var1);
            num2 = atoi(var2);
            answer = (num1 - num2);
            printf("%d\n", answer);
        }
        
        else if((strcmp(opptr, "div"))==0){
            num1 = atoi(var1);
            num2 = atoi(var2);
            if(num2 == 0){
                printf("infinity\n");
            }
            else{
            answer = (num1 / num2);
            printf("%d\n", answer);
            }
        }

        else if((strcmp(opptr, "mult"))==0){
            num1 = atoi(var1);
            num2 = atoi(var2);
            answer = (num1 * num2);
            printf("%d\n", answer);
        }
        else{
        printf("invalid command\n");
        goto again;
        }

    }
    
}
